﻿Imports System
Imports System.Workflow.ComponentModel

Namespace System.Workflow.Activities
    <Serializable> _
    Friend Class EventActivitySubscription
        Inherits StateMachineSubscription
        ' Methods
        Protected Overrides Sub Enqueue(ByVal context As ActivityExecutionContext)
            StateMachineExecutionState.Get(StateMachineHelpers.GetRootState(DirectCast(context.Activity, StateActivity))).SubscriptionManager.Enqueue(context, Me.QueueName)
        End Sub

        Friend Overrides Sub ProcessEvent(ByVal context As ActivityExecutionContext)
            Dim state As StateMachineExecutionState = StateMachineExecutionState.Get(StateMachineHelpers.GetRootState(DirectCast(context.Activity, StateActivity)))
            Dim action As New ExternalEventAction(Me.StateName, Me.EventDrivenName)
            state.EnqueueAction(action)
            state.ProcessActions(context)
        End Sub

        Friend Sub Subscribe(ByVal context As ActivityExecutionContext, ByVal state As StateActivity, ByVal eventActivity As IEventActivity)
            eventActivity.Subscribe(context, Me)
            Dim activity As Activity = DirectCast(eventActivity, Activity)
            Me._queueName = eventActivity.QueueName
            Me._eventActivityName = activity.QualifiedName
            Me._stateName = state.QualifiedName
            MyBase.SubscriptionId = Guid.NewGuid
            Dim parentEventDriven As EventDrivenActivity = StateMachineHelpers.GetParentEventDriven(eventActivity)
            Me._eventDrivenName = parentEventDriven.QualifiedName
        End Sub

        Friend Sub Unsubscribe(ByVal context As ActivityExecutionContext, ByVal eventActivity As IEventActivity)
            eventActivity.Unsubscribe(context, Me)
        End Sub


        ' Properties
        Friend ReadOnly Property EventActivityName As String
            Get
                Return Me._eventActivityName
            End Get
        End Property

        Friend ReadOnly Property EventDrivenName As String
            Get
                Return Me._eventDrivenName
            End Get
        End Property

        Friend ReadOnly Property QueueName As IComparable
            Get
                Return Me._queueName
            End Get
        End Property

        Friend ReadOnly Property StateName As String
            Get
                Return Me._stateName
            End Get
        End Property


        ' Fields
        Private _eventActivityName As String = String.Empty
        Private _eventDrivenName As String = String.Empty
        Private _queueName As IComparable
        Private _stateName As String = String.Empty
    End Class
End Namespace

